<%@ page import="org.apmap.base.Main" %>
<%@ page import="org.apmap.base.User" %>
<%@ page import="org.hibernate.Query" %>
<%@ page import="org.hibernate.Session" %>
<%@ page import="org.hibernate.Transaction" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/tags/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tags/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tags/struts-logic.tld" prefix="logic" %>
<html>
<head>
    <title>ApMap.org</title>
    <link rel="shortcut icon" href="/img/cubisthome.png" type="image/x-icon"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>


</head>
<body>

<%
    long userid = Long.parseLong(request.getParameter("userid"));
    Session hibsession = Main.getSession();
    final Query query = hibsession.createQuery("from User where id=:id");
    query.setLong("id", userid);
    List list = query.list();
    if (list.size() == 0) {
%>
<p>Нет такого пользователя</p>
<%
} else {
    User u = (User) list.get(0);


    if (session.getAttribute("user") != null && ((User) session.getAttribute("user")).getId() == u.getId()) {

        if (request.getParameter("name") != null) {
            u.getProfile().put("name", request.getParameter("name"));
            u.getProfile().put("contactInfo", request.getParameter("contactInfo"));

            Transaction transaction = hibsession.beginTransaction();
            hibsession.saveOrUpdate(u);
            transaction.commit();
        }


%>
<form action="profile.jsp" method="post">
    <input type="hidden" name="userid" value="<%=userid%>">
    имя: <input name="name" type="text" value="<%=u.getProfile().get("name") != null ? u.getProfile().get("name") : ""%>"><br/>
    контактная информация:<br/>
    <textarea rows="5" cols="100" name="contactInfo"><%=u.getProfile().get("contactInfo") != null ? u.getProfile().get("contactInfo") : ""%>
    </textarea><br/>
    <input type="submit" value="сохранить">
</form>
<%
} else {
%>
имя: <input type="text" value="<%=u.getProfile().get("name") != null ? u.getProfile().get("name") : ""%>" readonly="readonly" disabled="disabled"><br/>
контактная информация:<br/>
<textarea rows="5" cols="100" readonly="readonly" disabled="disabled"><%=u.getProfile().get("contactInfo") != null ? u.getProfile().get("contactInfo") : ""%>
</textarea><br/>
<%
        }
    }
    hibsession.close();

%>

</body>
</html>
